#!/bin/bash
#
# script to install fonts for Absolute Linux
# last edited Sunday, 08/30/2020
#  byPaul Sherman, GPL3

if [[ "$DISPLAY" && "$(which Xdialog 2>&1 | grep -v "which: no")" ]]; then
	dialog="Xdialog"
else
	dialog="dialog"
fi

if [ ! -e "$1" ]; then
	$dialog --title "Not Found" --msgbox "\n$1\nFile not found\n" 0 0
	exit
fi

filename=$(basename "$1")
fname=`echo "$filename" | tr "[:upper:]" "[:lower:]" | tr ' ' '_'`
fext=${fname##*.}

if [ $fext != "ttf" ] && [ $fext != "otf" ]; then
	$dialog --title "Not a TTF or OTF?" --msgbox "\n$1\nDoes not appear to be a TrueType or Opentype Font\n" 0 0
	exit
fi

if fc-list | grep -qi "$fname"; then
	$dialog --title "Already Installed" --msgbox "\n$fname appears to be installed already\n" 0 0
	exit 0
fi

# Stick font in global font dir if root.
# in $HOME/.fonts if a user

if [ `id -u` = 0 ]; then 
	if [ $fext == "ttf" ]; then
		cp "$1" /usr/share/fonts/TTF/$fname
		fc-cache /usr/share/fonts/TTF
	else
		cp "$1" /usr/share/fonts/OTF/$fname
		fc-cache /usr/share/fonts/OTF	
	fi
else
	if [ ! -d "$HOME/.fonts" ]; then
    	mkdir $HOME/.fonts
	fi
	cp $1 $HOME/.fonts/$fname
	fc-cache $HOME/.fonts
fi

if [ $? = 1 ]; then
	clear
	$dialog --title "Error..." \
        	--msgbox "$1\nwas not properly installed.\n\n" 0 0
else
	$dialog --title "Complete" \
        	--infobox "$1\nhas been installed.\n" 0 0 4000
fi
exit


